package com.yy.job.core.run.flow;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yy.job.common.dao.po.YYJobStepDO;
import com.yy.job.common.enums.ErrorCodeEnum;
import com.yy.job.common.enums.JobStepStatusEnum;
import com.yy.job.core.config.xml.JobStepXmlConfig;
import com.yy.job.core.domain.JobResult;
import com.yy.job.core.domain.JobStepDomain;
import com.yy.job.core.domain.context.JobStepContext;
import com.yy.job.core.run.process.IJobStepProcess;
import com.yy.job.spring.YYSpringBeanUtils;
import com.yy.job.utils.ThrowableUtils;
import com.yy.job.utils.exception.YYJobRuntimeException;
import com.yy.job.utils.exception.YYJobRuntimeExceptionUtils;

public class JobStepRunFlow extends AbsJobRunFlow<JobStepContext> {

	private Logger log = LoggerFactory.getLogger(JobStepsRunFlow.class);

	@Override
	public void flow(JobStepContext jobStepContext) {
		// 幂等判断
		boolean idempotent = this.idempotent(jobStepContext);
		if(idempotent) {
			return;
		}
				
		try {
			super.flow(jobStepContext);
		} catch (YYJobRuntimeException e) {
			log.error("handleJobStep YYJobRuntimeException failed", e);
			this.exception(jobStepContext, e);
		} catch (Exception e) {
			log.error("handleJobStep Exception failed", e);
			this.exception(jobStepContext, e);
		} finally {
			this.end(jobStepContext);
		}
	}
	
	@Override
	protected boolean idempotent(JobStepContext jobStepContext) {
		YYJobStepDO jobStepDO = YYSpringBeanUtils.getYYJobStepDOMapper().selectByPrimaryKey(jobStepContext.getJobStepDomain().getId());
		return JobStepStatusEnum.SUCCESS.getCode().equals(jobStepDO.getStepStatus()) 
			|| JobStepStatusEnum.SKIP.getCode().equals(jobStepDO.getStepStatus());
	}
	
	@Override
	protected void prepare(JobStepContext jobStepContext) {
		// 先更新执行次数和执行时间
		YYSpringBeanUtils.getJobManager().startJobStepExecute(jobStepContext);
	}

	@Override
	protected void check(JobStepContext jobStepContext) {
		JobStepDomain jobStepDomain = jobStepContext.getJobStepDomain();
		if (StringUtils.isBlank(jobStepDomain.getStepCode())) {
			YYJobRuntimeExceptionUtils.throwException(ErrorCodeEnum.CHECK_STEPCODE_NOT_EMPTY);
		}
		JobStepXmlConfig stepXmlConfig = jobStepContext.getJobConfig().findJobStepConfig(jobStepDomain.getStepCode());
		if (stepXmlConfig == null || StringUtils.isBlank(stepXmlConfig.getStepProcessBean())) {
			YYJobRuntimeExceptionUtils.throwException(ErrorCodeEnum.CHECK_STEPCODE_NOT_EXIST);
		}
		jobStepContext.setJobStepXmlConfig(stepXmlConfig);

		// 实现类是否存在
		IJobStepProcess jobStepProcess = YYSpringBeanUtils.getJobStepProcess(stepXmlConfig.getStepProcessBean());
		if (jobStepProcess == null) {
			YYJobRuntimeExceptionUtils.throwException(ErrorCodeEnum.CHECK_STEPPROCESS_NOT_EMPTY);
		}
		jobStepContext.setJobStepProcessBean(jobStepProcess);
	}

	@Override
	protected void run(JobStepContext jobStepContext) {
		JobStepDomain jobStepDomain = jobStepContext.getJobStepDomain();
		// 执行jobStep方法体
		JobResult jobResult = jobStepContext.getJobStepProcessBean().execute(jobStepDomain, jobStepDomain.getStepParams());
		jobStepContext.setJobResult(jobResult);
	}

	@Override
	protected void after(JobStepContext jobStepContext) {
		jobStepContext.setExecEndTime(new Date());
	}
	
	@Override
	protected void exception(JobStepContext jobStepContext, Exception e) {
		jobStepContext.setExecEndTime(new Date());
		if(e instanceof YYJobRuntimeException) {
			jobStepContext.setJobResult(new JobResult(false, e.getMessage(), null));
		} 
		else {
			jobStepContext.setJobResult(new JobResult(false, e.getMessage(), ThrowableUtils.getStackTrace(e)));
		}
	}
	
	@Override
	protected void end(JobStepContext jobStepContext) {
		YYSpringBeanUtils.getJobManager().endJobStepExecute(jobStepContext);
	}

}
